package com.leyou.item.web;

import com.leyou.common.dto.PageDTO;
import com.leyou.item.dto.SkuDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.dto.SpuDTO;
import com.leyou.item.dto.SpuDetailDTO;
import com.leyou.item.entity.Sku;
import com.leyou.item.entity.SpuDetail;
import com.leyou.item.service.SkuService;
import com.leyou.item.service.SpuDetailService;
import com.leyou.item.service.SpuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    SpuService spuService;
    @Autowired
    SpuDetailService spuDetailService;
    @Autowired
    SkuService skuService;
    //商品分页查询
    //- 接口地址： `/goods/spu/page`
    //- 请求方式： `GET`
    //- 请求格式：`form`
    //- 响应格式： `application/json`
    @GetMapping("spu/page")
    public ResponseEntity<PageDTO<SpuDTO>> queryByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer page,//当前页 设置默认1
            @RequestParam(value = "rows", defaultValue = "5") Integer rows,//当前页面显示数据数量 5
            @RequestParam(value = "saleable", required = false) Boolean saleable, //是否上架
            @RequestParam(value = "categoryId", required = false) Long categoryId, //商品分类id
            @RequestParam(value = "brandId", required = false) Long brandId, //品牌id
            @RequestParam(value = "id", required = false) Long id //商品spu的id
    ){
        PageDTO<SpuDTO> pag= spuService.queryByPag(page,rows,saleable,categoryId,brandId,id);
        return ResponseEntity.ok(pag);
    }

    //商品新增
    //- 接口地址： `/goods`
    //- 请求方式： `POST`
    //- 请求格式：`application/json`
    //- 响应格式： 无
    @PostMapping
    public ResponseEntity<Void> saveGoods(@RequestBody SpuDTO spuDTO){
        spuService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }
    //商品上下架
    //- 接口地址： `/goods/saleable`
    //- 请求方式： `PUT`
    //- 请求格式：`form`
    //- 响应格式： `无`
    @PutMapping("saleable")
    public ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id,
                                               @RequestParam ("saleable") boolean saleable){
            spuService.updateSaleable(id,saleable);
            return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //根据id查询商品
    //- 接口地址： `/goods/{id}`
    //- 请求方式： `GET`
    //- 请求格式：`path`
    //- 响应格式： `application/json`
    //
    //**响应示例**
    @GetMapping("{id}")
    public ResponseEntity<SpuDTO> querySpuById(@PathVariable("id")Long id){
           SpuDTO spuDTO =spuService.querySpuById(id);
        return ResponseEntity.ok(spuDTO);
    }
    //.商品修改
    //- 接口地址： `/goods`
    //- 请求方式： `PUT`
    //- 请求格式：`JSON`
    //- 响应格式： `无`
    @PutMapping
    public  ResponseEntity<Void> updateSpu(@RequestBody SpuDTO spuDTO){
        spuService.updateSpu(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }
    //根据id查询SpuDetail
    //- 接口地址： `/goods/spu/detail`
    //- 请求方式： `GET`
    //- 请求格式：`form`
    //- 响应格式： `JSON`
    @GetMapping("spu/detail")
    public ResponseEntity<SpuDetailDTO> queryById(@RequestParam Long id){
        SpuDetail byId = spuDetailService.getById(id);
        SpuDetailDTO spuDetailDTO = new SpuDetailDTO(byId);
        return ResponseEntity.ok(spuDetailDTO);
    }
    //根据SpuId查询SKU集合
    //- 接口地址： `/goods/sku/of/spu`
    //- 请求方式： `GET`
    //- 请求格式：`form`
    //- 响应格式： `JSON`
    @GetMapping("sku/of/spu")
    public ResponseEntity<List<SkuDTO>> querySkuBySpuId(@RequestParam Long id){
        List<Sku> spu_id = skuService.query().eq("spu_id", id).list();
        return ResponseEntity.ok(SkuDTO.convertEntityList(spu_id));
    }
    //根据id集合查询SKU集合
    //- 接口地址： `/goods/sku/list`
    //- 请求方式： `GET`
    //- 请求格式：`form`
    //- 响应格式： `JSON`
    @GetMapping("sku/list")
    public ResponseEntity<List<SkuDTO>> querySkusById(@RequestParam("ids") List<Long> ids){
        List<Sku> skuList = skuService.listByIds(ids);
        return ResponseEntity.ok(SkuDTO.convertEntityList(skuList));
    }

    //查询商品规格参数键值对
    //- 接口地址： `/goods/spec/value`
    //- 请求方式： `GET`
    //- 请求格式：`form`
    //- 响应格式： `application/json`
    @GetMapping("spec/value")
    public ResponseEntity<List<SpecParamDTO>> querySpecParam(@RequestParam("id")Long id,
                                                             @RequestParam(value = "searching",required = false) boolean searching){
    List<SpecParamDTO> responseEntity=spuDetailService.querySpecParam(id,searching);
            return ResponseEntity.ok(responseEntity);
    }
    //根据Id查询商品Spu
    //- 接口地址： `/goods/spu/{id}`
    //- 请求方式： `GET`
    //- 请求格式：`path`
    //- 响应格式： `application/json`
    @GetMapping("spu/{id}")
    public  ResponseEntity<SpuDTO> query1ById(@PathVariable("id")Long id){
        SpuDTO spuDTO = new SpuDTO(spuService.getById(id));
        return ResponseEntity.ok(spuDTO);
    }
    //减库存
    @PutMapping("/stock/minus")
    public ResponseEntity<Void> deductStock(@RequestBody Map<Long, Integer> cartMap){
        skuService.deductStock(cartMap);
        return ResponseEntity.noContent().build();
    }

}
